"""将倾斜的等距圆柱投影NAC DEM取其中横平竖直的部分作为规范数据"""

import cv2
import math
import tqdm

MOON_RADIUS = 1737.4 * 1000  # m
BATCH_SIZE = 1000
## for M1348581418LE_DEM
# LATITUDE_RANGE = 1.935  # degree
# LONGITUDE_RANGE = 0.5  # degree
# X_PIXEL_RANGE = 58235
# Y_PIXEL_RANGE = 15042


# def simple_cylinder(lat, lon, lat0=44.43, lon0=-52.24):
#     x = (lat0 - lat) * X_PIXEL_RANGE / LATITUDE_RANGE
#     y = (lon - lon0) * Y_PIXEL_RANGE / LONGITUDE_RANGE
#     return x, y

# ## for M1173414625_DEM
# LATITUDE_RANGE = 45.2692 - 42.8360  # degree
# LONGITUDE_RANGE = -51.4259 - (-52.3142)  # degree
# X_PIXEL_RANGE = 49213
# Y_PIXEL_RANGE = 17886
## for M1173414625_DEM regularized
LATITUDE_RANGE = 45.2410 - 42.8613  # degree
LONGITUDE_RANGE = -51.6879 - (-52.0974)  # degree
X_PIXEL_RANGE = 48131
Y_PIXEL_RANGE = 8264


def simple_cylinder(lat, lon, lat0=45.2692, lon0=-52.3142):
    x = (lat0 - lat) * X_PIXEL_RANGE / LATITUDE_RANGE
    y = (lon - lon0) * Y_PIXEL_RANGE / LONGITUDE_RANGE
    return x, y


def box_boundry(lat1, lon1, lat2, lon2):
    x1, y1 = simple_cylinder(lat1, lon1)
    x2, y2 = simple_cylinder(lat2, lon2)
    return x1, y1, x2, y2


def simple_cylinder_R(d, lat):
    D_x = math.degrees(d / MOON_RADIUS) * X_PIXEL_RANGE / LATITUDE_RANGE
    D_y = (
        math.degrees(d / MOON_RADIUS)
        * Y_PIXEL_RANGE
        / LONGITUDE_RANGE
        / math.cos(math.radians(lat))
    )

    return D_x / 2, D_y / 2


if __name__ == "__main__":
    FILE_PATH = "/disk527/DataDisk/a804_cbf/datasets/lunar_crater/M1173414625_DEM.tif"
    LABEL_DATA_PATH = (
        "/disk527/DataDisk/a804_cbf/datasets/lunar_crater/Qian_chang_e_5_annotation.csv"
    )

    SUBFIG_DIR = "/disk527/sdb1/a804_cbf/datasets/lunar_crater"
    img = cv2.imread(FILE_PATH, cv2.IMREAD_UNCHANGED)
    x1, y1, x2, y2 = box_boundry(
        lat1=45.2410, lon1=-52.0974, lat2=42.8613, lon2=-51.6879
    )
    img = img[round(x1) : round(x2), round(y1) : round(y2)]
    # cv2.imwrite(
    #     "/disk527/DataDisk/a804_cbf/datasets/lunar_crater/regularized_M1173414625_DEM.tif",
    #     img,
    # )
    img = (img - img.min()) / (img.max() - img.min()) * 255
    img = cv2.cvtColor(img.astype("uint8"), cv2.COLOR_GRAY2BGR)
    # 从标注文件中获取某一行
    with open(LABEL_DATA_PATH, "r") as f:
        # 有部分行只有圆的数据，没有椭圆的数据
        # 流式读取，第一行是标题，并使用标题的字段名作为数据变量名
        header = f.readline()
        header = header.strip().split(",")
        for line_ in tqdm.tqdm(f):
            line = line_.strip().split(",")
            # lon = float(line[1])
            # lat = float(line[2])
            # d = float(line[3]) * 1000
            lon = float(line[0]) - 360
            lat = float(line[1])
            d = float(line[2])
            x, y = simple_cylinder(lat, lon)
            d_x, d_y = simple_cylinder_R(d, lat)
            img = cv2.ellipse(
                img,
                (round(y), round(x)),
                (round(d_y), round(d_x)),
                0,
                0,
                360,
                color=(0, 0, 255),
                thickness=1,
            )
    # img = img[round(x1) : round(x2), round(y1) : round(y2)]
    cv2.imwrite("test.png", img)
